package com.itheima.controller;


import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderInfoVo;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CheckGroupYYService;
import com.itheima.utils.JedisUtil;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/checkgroupYY")
public class CheckGroupYYController {
    @Autowired
    JedisUtil jedisUtil;
    @Reference(cluster = "failfast")
    CheckGroupYYService checkGroupYYService;
    @RequestMapping("/addYY")
    public Result addYY(@RequestBody OrderInfoVo orderInfoVo, HttpServletRequest request){

        //校验验证码
        String validateCode = orderInfoVo.getValidateCode();
        String telephone = orderInfoVo.getTelephone();
        String redisCode = jedisUtil.get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
        if (StrUtil.isBlank(redisCode) || !redisCode.equals(validateCode)) {
            return Result.error("验证码错误");
        }

        //把预约类型改成电话预约
        orderInfoVo.setOrderType(Order.ORDERTYPE_TELEPHONE);

        Result result = null;
        result = checkGroupYYService.addYY(orderInfoVo);
        return result;

    }

    @RequestMapping("/send4Order")
    public Result send4Order(String telephone) {
        //生成验证码
        Integer code = ValidateCodeUtils.generateValidateCode(4);
        //发送验证码
//        SmsUtil.sendSmsCode(telephone, String.valueOf(code));
        //把验证码存入redis(提交预约的时候需要和用户比对)
        jedisUtil.add(telephone + RedisMessageConstant.SENDTYPE_ORDER, 60 * 5 * 100, String.valueOf(code));
        return Result.success("");
    }
    @RequestMapping("/findPages")
    public PageResult findPages(@RequestBody QueryPageBean queryPageBean) {
        return checkGroupYYService.findPages(queryPageBean);
    }
    @RequestMapping("/findAll")
    public Result findAll() {
        List<Setmeal> setmeals = checkGroupYYService.findAll();
        return Result.success("",setmeals);
    }
    @RequestMapping("/delete")
    public Result delete(@RequestBody OrderInfoVo orderInfoVo){
        checkGroupYYService.delete(orderInfoVo);
        return Result.success("删除订单成功");
    }
}
